package example;

import java.io.ByteArrayOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class Test {
    public static void main(String[] args) throws IOException {
        //最短路径
        String route = "http://localhost:4567/route?start_lon=114.2222&start_lat=30.5008&end_lon=114.5360&end_lat=30.3818";
        //前缀匹配
        String searchForPrefixWords = "http://localhost:4567/search?term=武汉";
        //搜索位置
        String searchForLoaction = "http://localhost:4567/search?term=武汉市少儿图书馆&full=true";
        InputStream in = getInputStream(searchForLoaction);
        System.out.println(getResult(in, "utf-8"));
    }

    public static InputStream getInputStream(String path) throws IOException {
        URL url = new URL(path);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();

        con.setRequestMethod("GET");
        con.setConnectTimeout(5000);
        con.setDoInput(true);

        InputStream in = null;
        in = con.getInputStream();
        return in;
    }

    public static String getResult(InputStream in, String code) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] b = new byte[1024];
        int len = 0;
        while ((len = in.read(b)) != -1) {
            baos.write(b, 0, len);
        }
        return new String(baos.toByteArray(), code);
    }

}
